let _that;

function checkAuth(that) {
  _that = that;
  console.log("checkAuth that  ==> wx.getSetting ")
  wx.getSetting({
    success: function (res) {
      if (res.authSetting['scope.userInfo']) {
        console.log("有res.authSetting['scope.userInfo']")
        _that.setData({
          authStatus: true
        }) 

      } else {
        console.log("没有res.authSetting['scope.userInfo']")
        _that.setData({
          authStatus: false
        })
      }
    },
    fail: function (res) {
      console.log("wx.getSetting ==>> fail")
      _that.setData({
        authStatus: false
      })
    }
  });
}


function getUserInfo(e) {
  console.log('************** ==>>', e);
  if (e.detail.userInfo) {
    console.log('获取微信用户信息 ==>>', e);
    var nickName = e.detail.userInfo.nickName;
    var avatarUrl = e.detail.userInfo.avatarUrl;
    console.log("getUserInfo: nickName && avatarUrl ==> ", nickName, avatarUrl)
    getChangeUserInfo(nickName, avatarUrl);
    _that.setData({
      authStatus: true
    }) 

  } else {
    console.log("拒绝授权")
    _that.setData({
      authStatus: false
    }) 

  }
}

function getChangeUserInfo(nickName, avatarUrl) {
  getApp().util.request({
    'url': 'entry/wxapp/update',
    'cachetime': '30',
    'method': 'POST',
    'data': {
      nickName: nickName,
      avatarUrl: avatarUrl
    },
    success: function (res) {
      // console.log("entry/wxapp/update ==>", res)
      if (!res.data.errno) { 
      }
    },
    fail: function (res) {
      console.log("getChangeUserInfo fail ==>>") 
    }
  })
}

module.exports = {
  checkAuth: checkAuth,
  getUserInfo: getUserInfo
};


